<script src="ckeditor/ckeditor.js" type="text/javascript"></script>
<? // Config
$tableCategoryConfig = 'tbl_product_category';
$tableConfig         = 'tbl_product';
$actConfig           = 'product';
$parentWhereConfig   = 'parent<>0';

$path = "../images/product";
$pathdb = "images/product";
?>
<script language="javascript">
function btnSave_onclick(){
	if(test_empty(document.frmForm.txtName.value)){
		alert('Hãy nhập "tên" !');
		document.frmForm.txtName.focus();
		return false;
	}
	if(test_integer(document.frmForm.txtSort.value)){
		alert('"Thứ tự sắp xếp" phải là số !');
		document.frmForm.txtSort.focus();
		return false;
	}	
	return true;
}
</script>

<? $errMsg =''?>
<?
if (isset($_POST['btnSave'])){
	$code          = isset($_POST['txtCode']) ? trim($_POST['txtCode']) : '';
	$name          = isset($_POST['txtName']) ? trim($_POST['txtName']) : '';
	$parent        = $_POST['ddCat'];
	$price         = isset($_POST['txtPrice']) ? trim($_POST['txtPrice']) : 0;
	$detail_short  = isset($_POST['txtshort']) ? trim($_POST['txtshort']) : '';
	$detail        = isset($_POST['txtlong']) ? trim($_POST['txtlong']) : '';
	$sort          = isset($_POST['txtSort']) ? trim($_POST['txtSort']) : 0;
	$status        = $_POST['chkStatus']!='' ? 1 : 0;
	$status1       = $_POST['chkStatus1']!='' ? 1 : 0;
	$pro_new       = $_POST['chkStatus2']!='' ? 1 : 0;
	$pro_sell      = $_POST['chkStatus3']!='' ? 1 : 0;
	$checkb         = @implode(',', $_POST['checkb']);  
	
	$catInfo       = getRecord($tableCategoryConfig, 'id='.$parent);
	$lang          = $catInfo['lang'];

	if ($name=="") $errMsg .= "Hãy nhập tên danh mục !<br>";
	//$errMsg .= checkUpload($_FILES["txtImage"],".jpg;.gif;.bmp;.png",1024*1024,0);
	//$errMsg .= checkUpload($_FILES["txtImageLarge"],".jpg;.gif;.bmp;.png",2048*1024,0);

	if ($errMsg==''){
		if (!empty($_POST['id'])){
			$oldid = $_POST['id'];
			$sql = "update ".$tableConfig." set code='".$code."',name='".$name."', parent='".$parent."',detail_short='".$detail_short."',detail='".$detail."', sort='".$sort."', status='".$status."', status1='".$status1."', pro_new='".$pro_new."', pro_sell='".$pro_sell."',checksao='".$checkb."', last_modified=now(), lang='".$lang."', price='".$price."' where id='".$oldid."'";
		}else{
			$sql = "insert into ".$tableConfig." (code, name, parent,detail_short, detail, sort, status, status1, pro_new, pro_sell, checksao, date_added, last_modified, lang, price,luotxem) values ('".$code."','".$name."','".$parent."','".$detail_short."','".$detail."','".$sort."','".$status."','".$status1."','".$pro_new."','".$pro_sell."','".$checkb."',now(),now(),'".$lang."','".$price."',1)";
		}
		if (@mysql_query($sql,$conn)){
			if(empty($_POST['id'])) $oldid = mysql_insert_id();
			$r = getRecord($tableConfig,"id=".$oldid);
            if($_POST['txtImage'] != ''){
                $sqlUpdateField = " image='".$_POST['txtImage']."' ,";
            }
			// txtImageLarge
            if($_POST['txtImageLarge'] != ''){
                $sqlUpdateField .= " image_large='".$_POST['txtImageLarge']."' ";
            }
			if($sqlUpdateField!='')	{
				$sqlUpdate = "update ".$tableConfig." set $sqlUpdateField where id='".$oldid."'";
				mysql_query($sqlUpdate,$conn);
			}
		}else{
			$errMsg = "Không thể cập nhật !";
		}
	}

	if ($errMsg == ''){
		echo '<script>window.location="./?act='.$actConfig.'&cat='.$_REQUEST['cat'].'&page='.$_REQUEST['page'].'&code=1"</script>';
        }                
}else{
	if (isset($_GET['id'])){
		$oldid=$_GET['id'];
		$page = $_GET['page'];
		$sql = "select * from ".$tableConfig." where id='".$oldid."'";
		if ($result = @mysql_query($sql,$conn)) {
			$row=@mysql_fetch_array($result);
			$code          = $row['code'];
			$name          = $row['name'];
			$parent        = $row['parent'];
			$subject       = $row['subject'];
			$price         = $row['price'];
			$detail_short  = $row['detail_short'];
			$detail        = $row['detail'];
			$image         = $row['image'];
			$image_large   = $row['image_large'];
			$sort          = $row['sort'];
			$status1       = $row['status1'];
			$status2       = $row['pro_new'];
			$status3       = $row['pro_sell'];
			$checkb         = $row['checksao'];
			$status        = $row['status'];
			$date_added    = $row['date_added'];
			$last_modified = $row['last_modified'];
			//$price         = $row['price'];
		}
	}
}

?>
<pre id="idTemporary1" name="idTemporary1" style="display:none">
<? if(isset($detail_short)){echo $detail_short;}?>
</pre>

<pre id="idTemporary2" name="idTemporary2" style="display:none">
<? if(isset($detail)){echo $detail;}?>
</pre>

<form method="post" name="frmForm" enctype="multipart/form-data" action="./">
<input type="hidden" name="act" value="<?=$actConfig?>_m">
<input type="hidden" name="id" value="<?=$_REQUEST['id']?>">
<input type="hidden" name="page" value="<?=$_REQUEST['page']?>">
<table border="1" cellpadding="1" cellspacing="1" bordercolor="#C9C9C9" width="100%">
	<tr>
    	<td>
    		<table border="0" cellpadding="1" cellspacing="1"  bordercolor="#111111" width="100%">
				<tr><td height="15px"></td></tr>
				<tr>
					<td colspan="3" align="center">
						<table width="100%">
							<? if($image!='' || $image_large!=''){?>
							<tr>
								<td width="15%"></td>
								<td width="40%" align="center" class="smallfont">
<? if ($image!=''){ echo '<img border="0" src="../images/product/images/'.$image.'" width="100"><br><br>Hình (kích thước nhỏ)';}?>
								</td>
								
								<td width="40%" align="center" class="smallfont">
<? if ($image_large!=''){ echo '<img border="0" src="../images/product/images/'.$image_large.'" width="100"><br><br>Hình (kích thước lớn)';}?>
								</td>
								<td width="15%"></td>
							</tr>
							<? }else{echo '<tr><td colspan="3" class="smallfont" align="center">Chưa có hình ảnh !</td></tr>';}?>
							<tr><td colspan="4" height="15"></td></tr>
							<tr><td colspan="4" height="1" bgcolor="#999999"></td></tr>
							<tr><td colspan="4" height="15"></td></tr>
						</table>
					</td>
				</tr>
        		
				<tr>
        			<td width="15%" class="smallfont" align="right">Mã sản phẩm</td>
        			<td width="1%" class="smallfont" align="center"></td>
        			<td width="83%" class="smallfont">
						<input value="<?=$code?>" type="text" name="txtCode" class="textbox" size="34">
					</td>
      			</tr>
				
				<tr>
					<td width="15%" class="smallfont" align="right">Tên sản phẩm</td>
					<td width="1%" class="smallfont" align="center"><font color="#FF0000">*</font></td>
					<td width="83%" class="smallfont">
						<input value="<?=$name?>" type="text" name="txtName" class="textbox" size="34">
					</td>
				</tr>
				
				<tr>
					<td width="15%" class="smallfont" align="right">Gi&aacute; sản phẩm</td>
					<td width="1%" class="smallfont" align="center"></td>
					<td width="83%" class="smallfont">
						<input value="<?=$price?>" type="text" name="txtPrice" class="textbox" size="34"> <?=$currencyUnit?>
					</td>
				</tr>
                
                <tr>
					<td width="15%" class="smallfont" align="right">Hình ảnh(nhỏ)</td>
					<td width="1%" class="smallfont" align="center"></td>
					<td width="83%" class="smallfont">
                        <input id="xFilePath" name="txtImage" type="text" size="60" />
                        <input type="button" value="Browse Server" onclick="BrowseServer();" />
                        <script type="text/javascript">

                        function BrowseServer()
                        {
                        	var finder = new CKFinder();
                        	finder.basePath = '../';	// The path for the installation of CKFinder (default = "/ckfinder/").
                        	finder.selectActionFunction = SetFileField;
                        	finder.popup();
                        }
                        function SetFileField( fileUrl )
                        {
                        	document.getElementById( 'xFilePath' ).value = fileUrl.split("/")[4];
                        }
                       	</script>
                    </td>
				</tr>
                
				<tr>
					<td width="15%" class="smallfont" align="right">Hình ảnh(lớn)</td>
					<td width="1%" class="smallfont" align="center"></td>
					<td width="83%" class="smallfont">
                        <input id="xFilePathLarge" name="txtImageLarge" type="text" size="60" />
                        <input type="button" value="Browse Server" onclick="BrowseServerL();" />
                        <script type="text/javascript">

                        function BrowseServerL()
                        {
                        	var finder = new CKFinder();
                        	finder.basePath = '../';
                        	finder.selectActionFunction = SetFileFieldL;
                        	finder.popup();
                        }
                        function SetFileFieldL( fileUrl )
                        {
                        	document.getElementById( 'xFilePathLarge' ).value = fileUrl.split("/")[4];
                        }
                       	</script>
                    </td>
				</tr>
				<tr>
					<td width="15%" class="smallfont" align="right">Mô tả ngắn </td>
					<td width="1%" class="smallfont" align="center"></td>
					<td width="83%" class="smallfont">
                     <textarea name="txtshort" cols="80" rows="10" id="txtshort"><?=$detail_short?></textarea>	
                     <script type="text/javascript">addCkeditor("txtshort");</script>				
					 </td>
				</tr>
				
				<tr>
					<td width="15%" class="smallfont" align="right">Thông tin chi tiết</td>
					<td width="1%" class="smallfont" align="center"></td>
					<td width="83%" class="smallfont">
						<textarea name="txtlong" cols="80" rows="10" id="txtlong"><?=$detail?></textarea>
                        <script type="text/javascript">addCkeditor("txtlong");</script>
					</td>
				</tr>
				
				<tr>
					<td width="15%" class="smallfont" align="right">Thu&#7897;c Danh m&#7909;c</td>
					<td width="1%" class="smallfont" align="center"></td>
					<td width="83%" class="smallfont">
						<?=comboCategory('ddCat',getArrayCategory($tableCategoryConfig),'smallfont',$parent,0)?>
					</td>
				</tr>
				<tr>
					<td width="15%" class="smallfont" align="right">Trạng thái mới</td>
					<td width="1%" class="smallfont" align="center"></td>
					<td width="83%" class="smallfont">
						<input type="checkbox" name="chkStatus1" value="on" <?=$status1>0?'checked':''?>>
						&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Sản phẩm mới
						<input type="checkbox" name="chkStatus2" value="on" <?=$status2>0?'checked':''?>>
						&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Sản phẩm bán chạy
						<input type="checkbox" name="chkStatus3" value="on" <?=$status3>0?'checked':''?>>
					</td>
				</tr>
				<tr>
					<td width="15%" class="smallfont" align="right"><img src="../images/star_1.gif" width="13" height="12" /><img src="../images/star_1.gif" width="13" height="12" /><img src="../images/star_1.gif" width="13" height="12" /> <img src="../images/star_1.gif" width="13" height="12" /><img src="../images/star_1.gif" width="13" height="12" /></td>
					<td width="1%" class="smallfont" align="center"></td>
					<td width="83%" class="smallfont">
						1<input type="checkbox" name="checkb[]" value="1" <?=$checkb==1?'checked':''?>>
						&nbsp;&nbsp;&nbsp;&nbsp;
						2<input type="checkbox" name="checkb[]" value="2" <?=$checkb==2?'checked':''?>>
						&nbsp;&nbsp;&nbsp;&nbsp;
						3<input type="checkbox" name="checkb[]" value="3" <?=$checkb==3?'checked':''?>>
						&nbsp;&nbsp;&nbsp;&nbsp;
						4<input type="checkbox" name="checkb[]" value="4" <?=$checkb==4?'checked':''?>>
						&nbsp;&nbsp;&nbsp;&nbsp;
						5<input type="checkbox" name="checkb[]" value="5" <?=$check==5?'checked':''?>>
					</td>
				</tr>
				<tr>
					<td width="15%" class="smallfont" align="right">Thứ tự sắp xếp</td>
					<td width="1%" class="smallfont" align="right"></td>
					<td width="83%" class="smallfont">
						<input value="<?=$sort?>" type="text" name="txtSort" class="textbox" size="34">
					</td>
				</tr>
				
				<tr>
					<td width="15%" class="smallfont"></td>
					<td width="1%" class="smallfont" align="center"></td>
					<td width="83%" class="smallfont">
						<input type="submit" name="btnSave" VALUE="Cập nhật" class="button" onclick="return btnSave_onclick()">
						<input type="reset" class=button value="Nhập lại">
					</td>
				</tr>
			</table>
		</td>
	</tr>
</table>
</form>
<? if($errMsg!=''){echo '<p align=center class="err">'.$errMsg.'<br></p>';}?>
